package com.xbongbong.paas.service.field.impl;

import com.xbongbong.paas.enums.VisibleTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.VisibleRulePoJo;
import com.xbongbong.saas.enums.ShowTypeEnum;

import java.util.ArrayList;
import java.util.Objects;

/**
 * @author：吴峰
 * @date：2020/07/13 11:13
 */
public class BaseOptimization {

    /**
     * 除去可新增、可删除高级属性
     * @param entity
     */
    protected void removeAddRuleAndRemoveRule(FieldAttrEntity entity) {
        entity.setAddRule(null);
        entity.setAdd(null);
        entity.setAddAdvanceEnable(null);
        entity.setRemoveRule(null);
        entity.setRemove(null);
        entity.setRemoveAdvanceEnable(null);
        entity.setMultiple(null);

        entity.setShowAdd(null);
        entity.setShowRemove(null);
    }
    /**
     * 除去items
     * @param entity
     */
    protected void removeItems(FieldAttrEntity entity) {
        entity.setItems(null);
        entity.setIsDictionary(null);
    }

    /**
     * 除去数字相关属性
     * @param entity
     */
    protected void removeNumericalLimits(FieldAttrEntity entity) {
        entity.setNumericalLimits(null);
        entity.setNumericalLimitsFlag(null);
        entity.setIntegerOnly(null);
        entity.setAccuracy(null);
        entity.setSummaryFlag(null);
        entity.setNumberType(null);
    }
    /**
     * 除去默认值
     * @param entity
     */
    protected void removeDefaultAttr(FieldAttrEntity entity) {
        entity.setDefaultAttr(new DefaultAttrPoJo());
    }
    /**
     * 去掉文件和图片
     * @param entity
     */
    protected void removeOfImageAndFile(FieldAttrEntity entity) {
        entity.setOnlyCamera(null);
        entity.setOnlyOneFile(null);
        entity.setMaxFileCount(null);
        entity.setAutoCompress(null);
    }

    protected void removeDateType(FieldAttrEntity entity) {
        entity.setDateType(null);
    }

    protected void removeLineType(FieldAttrEntity entity) {
        entity.setLineType(null);
    }

    protected void removeEditRule(FieldAttrEntity entity) {
        entity.setEditableAdvanceEnable(null);
        entity.setEditableRule(null);
    }

    /**
     *
     * @param entity
     */
    protected void removeOfHideField(FieldAttrEntity entity) {
        if (Objects.equals(1, entity.getEditHide()) && Objects.equals(ShowTypeEnum.NONE.getCode(), entity.getShowType())) {
            removeDefaultAttr(entity);
            removeDateType(entity);
            removeLineType(entity);
        }
    }

    /**
     * 公共处理方法
     * @param entity
     */
    protected void commonHandle(FieldAttrEntity entity) {
        if (Objects.equals(1, entity.getIsRedundant())) {
            entity.setRedFlag(null);
            entity.setCancelFlag(null);
            entity.setIsSingleRow(null);
            entity.setDesignEditable(null);
        }
        if (Objects.equals(entity.getShowEditable(), 0)) {
            // 不显示可编辑
            removeEditRule(entity);
        }
        VisibleRulePoJo visibleRulePoJo = entity.getVisibleScopeRule();
        boolean showEditable = Objects.isNull(entity.getEditHide()) || Objects.equals(0, entity.getEditHide());
        if (Objects.nonNull(visibleRulePoJo)) {
            visibleRulePoJo.setUserList(new ArrayList<>());
            entity.setVisibleScopeRule(visibleRulePoJo);
        }else if (showEditable){
            // 显示在界面上，且高级可见为null，赋初始值
            entity.setVisibleScopeEnable(0);
            visibleRulePoJo = new VisibleRulePoJo();
            visibleRulePoJo.setType(VisibleTypeEnum.NOT_SHOW_ANYONE.getType());
            entity.setVisibleScopeRule(visibleRulePoJo);
        }
    }

    protected void removeDetailAddress(FieldAttrEntity entity) {
        entity.setShowDetailAddress(null);
    }
}
